package com.heima.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel>implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //参数校验
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

        //构建查询条件,执行查询


        IPage<AdChannel> pageParam = new Page<>(dto.getPage(),dto.getSize());

        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        IPage<AdChannel> page = page(pageParam, queryWrapper);

        if(StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
        if(dto.getStatus()!=null){
            queryWrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        IPage<AdChannel> result = page(page,queryWrapper);

        //封装返回结果
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal(),result.getRecords());

        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //参数校验
        if(adChannel==null||!StringUtils.isNotBlank(adChannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //执行新增
        adChannel.setCreatedTime(new Date());
        if(adChannel.getIsDefault()){
            LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AdChannel::getIsDefault,1);
            AdChannel one = getOne(lambdaQueryWrapper);
            if(one!=null){
                one.setIsDefault(false);
                updateById(one);
            }
        }



        List<AdChannel> list = list();
        for (AdChannel channel : list) {
            if(channel.getName().equals(adChannel.getName())){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }


//        if(1>0){
//            CustException.cust(AppHttpCodeEnum.NEED_ADMIND);
//        }
        save(adChannel);
        //返回结果

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }




    @Override
    public ResponseResult update(AdChannel adChannel) {
        if(adChannel==null||StringUtils.isNotBlank(adChannel.getName())||adChannel.getIsDefault()==null)
        {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if(adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //先查后改
        AdChannel idOne = getById(adChannel.getId());
        if(idOne==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(adChannel.getIsDefault()){
            LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AdChannel::getIsDefault,1);
            AdChannel one = getOne(lambdaQueryWrapper);
            if(one!=null){
                one.setIsDefault(false);
                updateById(one);
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult delete(Integer id) {
    //参数校验
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //执行校验,先查后删
        AdChannel byId = getById(id);
        if(byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(byId.getIsDefault()){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_STOP_RUNNING);
        }
        if(byId.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_STOP_RUNNING);
        }


        removeById(id);

        //返回结果

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult order(Integer ord) {


        return null;
    }
}
